package com.overlord.im.commons.support.threadpool.fixed;


import com.overlord.im.commons.support.threadpool.AbortPolicyWithReport;
import com.overlord.im.commons.support.threadpool.NamedInternalThreadFactory;
import com.overlord.im.commons.support.threadpool.ThreadPoolConstants;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Creates a thread pool that reuses a fixed number of threads
 *
 * @see java.util.concurrent.Executors#newFixedThreadPool(int)
 */
public class FixedThreadPool {

    public static ThreadPoolExecutor getThreadPool(String name, int threads, int queues) {
        return new ThreadPoolExecutor(threads, threads, 0, TimeUnit.MILLISECONDS,
                queues == 0 ? new SynchronousQueue<>() :
                        (queues < 0 ? new LinkedBlockingQueue<>()
                                : new LinkedBlockingQueue<>(queues)),
                new NamedInternalThreadFactory(name, true), new AbortPolicyWithReport(name));
    }

    public static ThreadPoolExecutor defaultThreadPool() {
        return getThreadPool(ThreadPoolConstants.DEFAULT_THREAD_NAME, ThreadPoolConstants.DEFAULT_THREADS,
                ThreadPoolConstants.DEFAULT_QUEUES);
    }

}
